Skip to content

Fix GH-19200: replace unchecked realloc/malloc with perealloc/pemalloc#21625

Closed
iliaal wants to merge 1 commit intophp:masterfrom
iliaal:fix/gh-19200-unchecked-realloc-malloc
Closed

Fix GH-19200: replace unchecked realloc/malloc with perealloc/pemalloc#21625
iliaal wants to merge 1 commit intophp:masterfrom
iliaal:fix/gh-19200-unchecked-realloc-malloc

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Apr 3, 2026

Summary

Several call sites use raw realloc()/malloc() without checking for NULL. When realloc() fails, it returns NULL and the original pointer is lost, crashing on the next dereference. Replaced with perealloc()/pemalloc() (persistent=1), which wrap the system allocator and call zend_out_of_memory() on failure for a clean exit.

Changes:

  • Zend/zend.c, zend_append_version_info() realloc
  • Zend/zend_API.c, zend_collect_module_handlers() (3 reallocs), zend_register_functions() (2 reallocs + 3 mallocs), do_register_internal_class() (1 malloc)
  • Zend/zend_inheritance.c, ce->interfaces realloc in 2 ZEND_INTERNAL_CLASS branches, simplified to single perealloc() matching the pattern at zend_implement_stringable()
  • ext/opcache/zend_accelerator_blacklist.c, blacklist entries realloc
  • main/network.c, gethostname_re() variants (3 mallocs + 3 reallocs)
  • main/php_ini.c, php_ini_scanned_files realloc
  • main/php_ini_builder.h, INI builder realloc
  • sapi/phpdbg/phpdbg.c, extension list realloc/malloc
  • sapi/phpdbg/phpdbg_prompt.c, code buffer realloc/malloc

Skipped zend_alloc.c (already handles this) and IR JIT code (third-party).

Fixes GH-19200, closes GH-17013

Copy link
Copy Markdown
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't object.

Copy link
Copy Markdown
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks correct, but please target master. I'm guessing other people would also appreciate the use of true over 1 (there has been some automated migration).

@iliaal iliaal changed the base branch from PHP-8.4 to master May 3, 2026 15:17
@iliaal iliaal force-pushed the fix/gh-19200-unchecked-realloc-malloc branch from cde419a to b934a5d Compare May 3, 2026 15:21
…lloc

Raw realloc() returns NULL on allocation failure, losing the original
pointer and causing a crash on the next dereference. pemalloc/perealloc
with persistent=true wrap the system allocator but call
zend_out_of_memory() on failure, giving a clean exit instead of an
undefined crash.

Converts all V701 locations from the PVS-Studio report and unchecked
malloc calls in zend_register_functions() (phpGH-17013). Skips
zend_alloc.c (already handled) and IR JIT code (third-party).

The zend_inheritance.c changes also simplify the realloc/erealloc
branch into a single perealloc() call, matching the existing pattern
at zend_implement_stringable().

Fixes phpGH-19200
Closes phpGH-17013
@iliaal iliaal force-pushed the fix/gh-19200-unchecked-realloc-malloc branch from b934a5d to e7a61cf Compare May 3, 2026 15:36
@iliaal
Copy link
Copy Markdown
Contributor Author

iliaal commented May 3, 2026

Retargeted to master and switched the persistent literals to true.

Copy link
Copy Markdown
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Please replace the huge AI generated description with something short and sensible. E.g. "Replace some malloc|realloc calls with OOM-safe (pemalloc|perealloc)(persistent: true) counterparts.

@iliaal
Copy link
Copy Markdown
Contributor Author

iliaal commented May 3, 2026

Looks good. Please replace the huge AI generated description with something short and sensible. E.g. "Replace some malloc|realloc calls with OOM-safe (pemalloc|perealloc)(persistent: true) counterparts.

I'll draft something short & to the point, need to rebase it all to one commit etc.. just want to see ASAN pass 1st

@iliaal iliaal closed this in e228395 May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not checked realloc and malloc result can result in unclean crash No check NULL after call malloc function

3 participants